package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Employee;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {
    // 注入分类管理
    @Autowired
    private CategoryMapper categoryMapper;

    // 注入菜品
    @Autowired
    private DishMapper dishMapper;

    // 注入套餐
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分类分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult page(CategoryPageQueryDTO dto) {
        // 1.设置分页参数
        PageHelper.startPage(dto.getPage(), dto.getPageSize());

        // 2.调用mapper的查询方法，并强转返回类型为Page
        Page<Category> page = categoryMapper.list(dto.getName(), dto.getType());

        // 3.封装PageResult对象返回
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 启用禁用分类
     * @param status
     * @param id
     */
    @Override
    public void setEnable(Integer status, Long id) {
        Category category = Category.builder()
                .status(status)
                .id(id)
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();

        categoryMapper.update(category);
    }

    /**
     * 修改分类
     * @param dto
     */
    @Override
    public void update(CategoryDTO dto) {
        Category category = new Category();
        // 属性拷贝
        BeanUtils.copyProperties(dto, category);

        //补充属性
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.update(category);
    }

    /**
     * 新增分类
     * @param dto
     * @return
     */
    @Override
    public void addCatgory(CategoryDTO dto) {
        Category category = new Category();
        // 属性拷贝
        BeanUtils.copyProperties(dto, category);
        category.setStatus(StatusConstant.DISABLE);

        //补充属性
//        category.setStatus(StatusConstant.DISABLE);
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.insert(category);
    }

    /**
     * 查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.listCategory(type);
    }

    /**
     * 删除分类
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        // 1.读取菜品中关联Id的个数
        Integer count = dishMapper.countByCategoryId(id);
        if (count > 0) {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        // 1.读取菜品中关联Id的个数
        count = setmealMapper.countByCategoryId(id);
        if (count > 0) {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        // 删除该分类
        categoryMapper.deleteById(id);
    }

}
